package com.example.studyPlatform.controller;

import com.example.studyPlatform.config.ThemeConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

import static com.example.studyPlatform.config.BaseConstant.HTML_SUFFIX;
import static com.example.studyPlatform.config.RoutingConstant.*;

@Controller
public class IndexController {
    ThemeConfig themeConfig;
    String url;

    public IndexController(ThemeConfig themeConfig, @Value("${spring.datasource.url}") String url) {
        this.themeConfig = themeConfig;
        this.url = url;
    }

    @GetMapping("/")
    public String index(Model model) {
        return themeConfig.render(INDEX);
    }

    @GetMapping(REGISTER + HTML_SUFFIX)
    public String register(Model model) {
        return themeConfig.render(REGISTER);
    }

    @GetMapping(LOGIN + HTML_SUFFIX)
    public String login(Model model) {
        model.addAttribute("uuid", UUID.randomUUID().toString());
        return themeConfig.render(LOGIN);
    }

    // 公开的动态页面
    @GetMapping(PUBLIC_PATH + "/*")
    public String publicPageMap(HttpServletRequest request) {
        String url = request.getRequestURI();
        return themeConfig.render(url);
    }

    @RequestMapping(USER_INDEX)
    String userPage(Model model) {
        return themeConfig.render(USER_INDEX);
    }
}
